#ifndef IMAGERECORD_H
#define IMAGERECORD_H

#include "ngcard.h"

#include <opencv2/core.hpp>
#include <QDateTime>
#include <QString>
#include <QMap>
#include <QElapsedTimer>
#include <QStateMachine>
#include <QState>
#include "QPoint"

enum StepProcess {
    Capture,
    Reprogress,
    Detect,
    Cv,
    PCcoreStep,
    Summary,
    total
};

class ImageRecord {

public:
    enum State {
        Running,
        Succeed,
        Fail,
    };
    enum DisplayImgStep{
        DisplayCap,
        DisplayPre,
        DisplayDefault
    };
    explicit ImageRecord();
    void setStepImage(StepProcess step, const cv::Mat& image , DisplayImgStep displayStep = DisplayDefault);
    void setFinalResult(const QString& result);
    cv::Mat getStepImage(StepProcess step) const;
    QString logOutResult();
    void setState(State state);
    State getState() const;
    cv::Mat& getResultImage();
    void setIsSucceeded(bool isSucceeded);
    void setIdentImage(cv::Mat img,cv::Rect2d roi);
    NgCard getNgCard();
    DisplayImgStep getdisplayStep() const {return displayStep_;};
    void setDefect(int defectKind);

    void setDetectStepTime(const StepProcess step, const QString info);

    void setMonitorRoi(cv::Rect2d rect);
    cv::Rect2d getMonitorRoi() const {return monitorRoi;};

    StepProcess getStepProcess() const {return m_endStepLast;};

    void setIdentROi(cv::Rect2d rect);
    cv::Rect2d getIdentROi() const {return identRoi;};  //相对与基准框

    void resetTimer();
    int getElapsedTime() const {return m_timer.elapsed(); };

    cv::Mat getCaptureImageGray() const {return m_captureImageGray;};
    void setCaptureImageGray(cv::Mat img);

    cv::Mat getReprogressImageGray() const {return m_reprogressImageGray;};
    void setReprogressImageGray(cv::Mat img);

    cv::Mat getidentImageGray() const {return m_identImageGray;};
    void setidentImageGray(cv::Mat img);

    cv::Mat getbaseImage() const {return m_baseImage;};
    void setbaseImage(cv::Mat img);

    cv::Mat getdrawPreoProcessImage() const {return m_drawPreoProcessImage;};
    void setdrawPreoProcessImage(cv::Mat img);

    QVariantMap getdetectStepTime() const{return detectStepTime;};
    QVariantMap getdetectStepInfo() const{return detectStepInfo;};

    void setdistanceLargeSmall(const float distanceToTop ,const float distanceToBottom);
    void getdistanceLargeSmall(float& distanceToTop,float& distanceToBottom);

    bool getIsSkipDisplay() const {return isSkipDisplay_;};
    void setIsSkipDisplay(bool isSkipDisplay);

    void setID(int id);
    int getID() const {return m_ID;};

    void setIdentRoi2src(cv::Rect2d identRoi2src_);

    QVector<cv::Mat> emphasizeCurentList;
    QVector<QPoint> emphasizeCurentPointList;
    cv::Mat grayImagePre ;
    cv::Mat imageRote ;
    float moveSpace = 0;
    bool isInROI = true;

    float largeSmallLastTop;
    float largeSmallLastBottom;
    double lenthDistance;
private:
    void initializeStateMachine();
    void logStateChange(State state);
    bool isSkipDisplay_;

    cv::Mat m_originalImage;
    cv::Mat m_reprogressImage;
    cv::Mat m_detectImage;
    cv::Mat m_cvImage;
    cv::Mat m_outputImage;
    cv::Mat identImage;
    cv::Mat m_identImageGray;
    cv::Mat m_reprogressImageGray;
    cv::Mat m_captureImageGray;
    cv::Mat m_baseImage;
    cv::Mat m_drawPreoProcessImage;

    DisplayImgStep displayStep_;
    int lastTime;
    int m_timesCapture;
    int m_timesReprogress;
    int m_timesDetect;
    int m_timesCv;
    int m_timesSummary;
    int m_PCcore;
    int m_timesFinal;
    int m_ID;

    float distanceToTop_;
    float distanceToBottom_;

    cv::Rect2d identRoi2src;
    cv::Rect2d identRoi;
    cv::Rect2d monitorRoi;

    QVariantMap detectStepInfo;
    QVariantMap detectStepTime;
    int m_finalResult;
    State m_state;
    StepProcess m_endStepLast;
    QDateTime m_captureTime;
    QElapsedTimer m_timer;
    NgCard ngcard_ ;

    // QStateMachine m_stateMachine;
    // QState m_stateRunning;
    // QState m_stateSucceed;
    // QState m_stateFail;
};

#endif
